package com.cardone.platform.configuration.controller;

import java.util.List;
import java.util.concurrent.Callable;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.dto.ReturnDataDto;
import com.cardone.common.util.ReturnDataUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.configuration.dto.DictionaryItemDto;
import com.cardone.platform.configuration.service.DictionaryItemService;

/**
 * 字典项
 *
 * @author yaohaitao
 */
@Deprecated
@Setter
@Slf4j
@RequestMapping("/configuration/dictionaryItem/")
@Controller
public class DictionaryItemController {
	/**
	 * 初始化数据绑定
	 *
	 * @param webDataBinder
	 *          数据绑定
	 */
	// @InitBinder
	// @Deprecated
	// public void initBinder(final WebDataBinder webDataBinder) {
	// }

	/**
	 * 字典项：删除
	 *
	 * @param deleteDictionaryItem
	 *          字典项对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Callable<ReturnDataDto<String>> deleteJson(final DictionaryItemDto deleteDictionaryItem) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(DictionaryItemService.class).deleteByIds(deleteDictionaryItem);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, DictionaryItemController.log);
				}
			}
		};
	}

	/**
	 * 字典项：分页查询
	 *
	 * @param findDictionaryItem
	 *          字典项对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "edit.html" })
	public void editHtml(final DictionaryItemDto findDictionaryItem, final Model model) {
		final DictionaryItemDto dictionaryItem = ContextHolder.getBean(DictionaryItemService.class).findById(DictionaryItemDto.class, findDictionaryItem);

		model.addAttribute("dictionaryItem", dictionaryItem);
	}

	/**
	 * 字典项：编辑
	 *
	 * @param findDictionaryItem
	 *          字典项对象
	 * @return 实体对象集，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findById.json" })
	@ResponseBody
	public Callable<ReturnDataDto<DictionaryItemDto>> findByIdJson(final DictionaryItemDto findDictionaryItem, final Model model) {
		return new Callable<ReturnDataDto<DictionaryItemDto>>() {
			@Override
			public ReturnDataDto<DictionaryItemDto> call() throws Exception {
				try {
					final DictionaryItemDto dictionaryItem = ContextHolder.getBean(DictionaryItemService.class).findById(DictionaryItemDto.class, findDictionaryItem);

					return ReturnDataUtils.newReturnData(dictionaryItem);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, DictionaryItemController.log);
				}
			}
		};
	}

	/**
	 * 字典项：查询
	 *
	 * @param findListDictionaryItem
	 *          字典项对象
	 * @return 字典项对象集合，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findListByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<List<DictionaryItemDto>>> findListByLikeCodeJson(final DictionaryItemDto findListDictionaryItem) {
		return new Callable<ReturnDataDto<List<DictionaryItemDto>>>() {
			@Override
			public ReturnDataDto<List<DictionaryItemDto>> call() throws Exception {
				try {
					final List<DictionaryItemDto> dictionaryItemList = ContextHolder.getBean(DictionaryItemService.class).findListByLikeCode(DictionaryItemDto.class, findListDictionaryItem);

					return ReturnDataUtils.newReturnData(dictionaryItemList);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, DictionaryItemController.log);
				}
			}
		};
	}

	/**
	 * 字典项：分页查询
	 *
	 * @param paginationDictionaryItem
	 *          字典项对象
	 * @return 分页，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "paginationByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<PaginationDto<DictionaryItemDto>>> paginationByLikeCodeJson(final DictionaryItemDto paginationDictionaryItem) {
		return new Callable<ReturnDataDto<PaginationDto<DictionaryItemDto>>>() {
			@Override
			public ReturnDataDto<PaginationDto<DictionaryItemDto>> call() throws Exception {
				try {
					final PaginationDto<DictionaryItemDto> dictionaryItemPagination = ContextHolder.getBean(DictionaryItemService.class).paginationByLikeCode(DictionaryItemDto.class, paginationDictionaryItem);

					return ReturnDataUtils.newReturnData(dictionaryItemPagination);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, DictionaryItemController.log);
				}
			}
		};
	}

	/**
	 * 字典项：分页查询
	 *
	 * @param paginationDictionaryItem
	 *          字典项对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "pagination.html" })
	public void paginationHtml(final DictionaryItemDto paginationDictionaryItem, final Model model) {
		final PaginationDto<DictionaryItemDto> dictionaryItemPagination = ContextHolder.getBean(DictionaryItemService.class).paginationByLikeCode(DictionaryItemDto.class, paginationDictionaryItem);

		model.addAttribute("dictionaryItemPagination", dictionaryItemPagination);
	}

	/**
	 * 字典项：保存
	 *
	 * @param saveDictionaryItem
	 *          字典项对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "save.json" })
	@ResponseBody
	public Callable<ReturnDataDto<String>> saveJson(final DictionaryItemDto saveDictionaryItem) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(DictionaryItemService.class).saveByIdOrCode(DictionaryItemDto.class, saveDictionaryItem);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, DictionaryItemController.log);
				}
			}
		};
	}
}
